package com.wframe.project.jnd.services.impl;

import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.wframe.project.jnd.dao.ProductDao;
import com.wframe.project.jnd.dao.PromotionDao;
import com.wframe.project.jnd.entity.*;
import com.wframe.project.jnd.services.PromotionService;
import com.wframe.project.jnd.vo.ActiveAreaProduct;
import com.wframe.project.jnd.vo.CartItem;
import com.wframe.project.jnd.vo.CartProductVO;
import com.wframe.project.jnd.vo.Product;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * Created by lwb on 2017/6/3.
 */
@Service
public class PromotionServiceImpl implements PromotionService {
    @Autowired
    private PromotionDao promotionDao;
    @Autowired
    private ProductDao productDao;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private final String ACTIVE_AREA = "ACTIVE_AREA_";

    @Override
    public Promotion findFlashSale(String promotionId) throws Exception {
        return promotionDao.findFlashSale(promotionId);
    }

    @Override
    public Promotion findBuySend(String promotionId) throws Exception {
        return promotionDao.findBuySend(promotionId);
    }

    @Override
    public Promotion findMoneyOff(String promotionId) {
        List<MoneyOffItem> items = new ArrayList<>();
        List<JdataBean> beans = promotionDao.findMoneyOff(promotionId);
        for (JdataBean bean : beans) {
            MoneyOffItem item = new MoneyOffItem();
            item.setId(bean.getInt("itemid"));
            item.setOffMoney(bean.getBigDecimal("off_money"));
            item.setDiscountMoney(bean.getBigDecimal("discount_money"));
            Double discountRate = bean.getDoubler("discount_rate");
            if (Objects.nonNull(discountRate)) {
                discountRate /= 10;
                item.setDiscountRate(discountRate);
            } else {
                item.setDiscountRate(discountRate);
            }
            items.add(item);
        }

        MoneyOffPromotion promotion = new MoneyOffPromotion();
        promotion.setId(beans.get(0).getInt("id"));
        promotion.setTime(beans.get(0).getInteger("time"));
        promotion.setDescription(beans.get(0).getString("description"));
        promotion.setItems(items);
        return promotion;
    }

    @Override
    public Promotion findPurchaseGift(String promotionId) {
        boolean flag = false;
        List<PurchaseGiftItem> items = promotionDao.findPurchaseGift(promotionId);
        for (PurchaseGiftItem item : items) {
            Integer number = item.getMinNumber();
            if (Objects.nonNull(number)) {
                flag = true;
            }
            item.setGifts(findGifts(item.getGiftIds(), item.getProductIds()));
        }

        PurchaseGiftPromotion promotion = new PurchaseGiftPromotion();
        promotion.setBuySend(flag);
        promotion.setId(Integer.parseInt(promotionId));
        promotion.setItems(items);
        return promotion;
    }

    private List<PurchaseGiftProduct> findGifts(String giftIds, String productIds) {
        List<PurchaseGiftProduct> gifts = new ArrayList<>();
        List<JdataBean> beans = promotionDao.findGifts(giftIds, productIds);
        for (JdataBean bean : beans) {
            PurchaseGiftProduct gift = new PurchaseGiftProduct();
            gift.setId(bean.getInt("giftid"));
            gift.setProductid(bean.getString("productid"));
            gift.setUnit(bean.getInt("unit"));
            gift.setUrl(bean.getString("photo"));
            gift.setName(bean.getString("name"));
            gifts.add(gift);
        }
        return gifts;
    }

    @Override
    public JdataBean findFlashSaleByProductId(String productid, String userid, int platform) {
        return promotionDao.findFlashSaleByProductId(productid, userid, platform);
    }

    @Override
    public PageResult findOnFlashSaleByPlatform(int platform, Long uid, int pagesize, int pageno) {
        return this.promotionDao.findOnFlashSaleByPlatform(platform, uid, pagesize, pageno);
    }

    @Override
    public boolean checkInventory(String promotionId) {
        return promotionDao.checkFlashSaleInventory(promotionId);
    }

    @Override
    public Integer getResidualNumber(String userid, String promotionid) {
        JdataBean history = promotionDao.findHistory(userid, promotionid);
        Integer time = history.getInteger("time");
        if (Objects.isNull(time)) {
            return time;
        }
        return time - history.getInt("history");
    }

    @Override
    public boolean isTimeLimit(int buyNum, String userid, String promotionid) {
        Integer residualNumber = getResidualNumber(userid, promotionid);
        if (Objects.isNull(residualNumber)) {
            return false;
        }
        return buyNum > residualNumber;
    }

    public JdataBean findBuySendVO(String promotionId) {
        return promotionDao.findBuySendVO(promotionId);
    }

    @Override
    public JdataBean findBuySendByProductId(String productid, String userid) {
        return promotionDao.findBuySendByProductId(productid, userid);
    }

    @Override
    public List<JdataBean> findByIds(String[] ids) {
        return promotionDao.findByIds(ids);
    }

    @Override
    public List<ActiveAreaProduct> findOnSalePromotions(Promotion.Type type, int platform, int weight, long number, TimeUnit unit, String userid) {
       /* String key = ACTIVE_AREA + type.name();
        List<ActiveAreaProduct> products = null;
        ValueOperations<String, String> stringValueOperations = redisTemplate.opsForValue();

        String value = stringValueOperations.get(key);
        if (StringUtils.isNotBlank(value)) {
            products = JSON.parseArray(value, ActiveAreaProduct.class);
        } else {
            products = promotionDao.findOnSalePromotions(type.ordinal(), platform, weight, Promotion.Priority.FIRST.ordinal(), userid);
            if (userid != null) {
                stringValueOperations.set(key, JSON.toJSONString(products), number, unit);
            }
        }*/
        return promotionDao.findOnSalePromotions(type.ordinal(), platform, weight, Promotion.Priority.FIRST.ordinal(), userid);
    }

    @Override
    public Map<String, String> findAll(String productid, String userid, int platform) {
        return promotionDao.findAll(productid, userid, platform);
    }

    @Override
    public Promotion findFlashSale(String userid, String promotionid) {
        return promotionDao.findFlashSale(userid, promotionid);
    }

    @Override
    public List<JdataBean> findAll(String userid, Set<String> ids, int platform) {
        return promotionDao.findAvailablePromotions(userid, ids, platform);
    }

    @Override
    public List<FlashSalePromotion> findFlashSale(String userid, Set<String> ids, int platform) {
        return promotionDao.findFlashSale(userid, ids, platform);
    }

    @Override
    public List<BuySendPromotion> findBuySend(String userid, Set<String> ids) {
        return promotionDao.findBuySend(userid, ids);
    }

    @Override
    public List<JdataBean> findSpecificPromotions(String userid, Set<Integer> ids, int platform) {
        Map<String, Object> parms = new HashMap<>();
        parms.put("userid", userid);
        parms.put("ids", ids);
        parms.put("platform", platform);
        return promotionDao.findSpecificPromotions(parms);
    }

}
